package A_review.review1_17;

import listNodePractice.Class347_1;

import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class Class347 {

    public int[] topKFrequent(int[] nums, int k){
        int len = nums.length;
        if (k>len){
            return  null;
        }
        int[] ans = new int[k];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < len; i++) {
            map.put(nums[i],map.getOrDefault(nums[i],0)+1);
        }

        PriorityQueue<Map.Entry<Integer,Integer>> priorityQueue = new PriorityQueue<>(
                //TODO:2024/1/17  这一块还必须的有
                        (entry1,entry2)->{
                            return entry1.getValue()-entry2.getValue();
                        }

        );

        for (Map.Entry<Integer,Integer> entry: map.entrySet()) {
            priorityQueue.offer(entry);
            if (priorityQueue.size()>k){
                priorityQueue.poll();
            }
        }
        for (int i = 0; i < k; i++) {
            ans[i]=priorityQueue.poll().getKey();
        }
        return  ans;
    }


    public static void main(String[] args) {
        Class347 class347_1 = new Class347();
        int[] nums={2,3,2,5,4,3};
        int[] ints = class347_1.topKFrequent(nums, 2);
        for (int a : ints
        ) {
            System.out.println(a);

        }
    }
}
